//
//  ContentView.swift
//  car
//
//  Created by 范俊伟 on 2024/1/10.
//

import SwiftUI


struct ContentView: View {
    @State private var title:String = "test";
    
    var bleManager = BLEManager();
    
    @State var isUserMotion = false;
    @State private var speed = 25.0;
    @State private var song = 1.0;
    let MinSpeed = 26.0
    @State private var isEditing = false
    
    @State private var data:SaveData = SaveData(x: 0, y: 0, beep: 0)
    func updateBLE(){
        var x = 0
        var y = 0
        if(self.speed<MinSpeed){
            y = self.data.y;
        }else{
            if(self.data.y > 0){
                y = Int(speed)
            }else if(self.data.y < 0){
                y = -Int(speed)
            }else{
                y=0
            }
        }
        
        if(self.data.x > 50){
            x = 100
        }else if(self.data.x < -50){
            x = -100
        }else{
            x = 0
        }
        
        print("UPDATE BLE: \(-y)#\(x)#\(data.beep)")
        self.bleManager.writeValue(data: "\(-y)#\(x)#\(data.beep)")
    }
    func lableText() -> String{
        if(speed<MinSpeed){
            return "自动"
        }else{
            return "\(Int(speed))"
        }
    }
    func songText() -> String{
        return "音乐\(Int(song))"
    }
    
    var body: some View {
        VStack {
            Spacer()
                .frame(height: 20.0)
            VStack{
                #if os(iOS)
                Toggle("使用陀螺仪", isOn: $isUserMotion)
                #endif
                HStack {
                    
                    Button(action: {}){
                        Text("喇叭").padding().foregroundColor(Color.white).background(Color.blue).cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                    }.buttonStyle(.plain)
                    .simultaneousGesture(
                        DragGesture(minimumDistance: 0)
                            .onChanged({ _ in
                                print("donw")
                                if self.data.beep == 0{
                                    self.data = SaveData(x: self.data.x, y: self.data.y, beep: Int(self.song))
                                    //                                    self.updateBLE()
                                }
                                
                            })
                            .onEnded({ _ in
                                print("up")
                                self.data = SaveData(x: self.data.x, y: self.data.y, beep: 0)
                                //                                self.updateBLE()
                            })
                        
                    )
                    //                    Button(){
                    //                        Text("喇叭")
                    //                            .padding().foregroundColor(Color.white).background(Color.blue).cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                    //                    }
                    
                    Spacer(minLength: 100)
                    Slider(
                        value: $song,
                        in: 1...4,
                        step: 1,
                        onEditingChanged: { editing in
                            
                            print("isEditing\(isEditing)")
                        }
                    )
                    Text("\(songText())")
                        .foregroundColor(.blue)
                }.padding()
                
                
            }.padding()
            RockerView(data:self.$data, isUserMotion: self.$isUserMotion)
            HStack{
                Text("速度")
                
                Slider(
                    value: $speed,
                    in: MinSpeed-1...100,
                    step: 1,
                    onEditingChanged: { editing in
                        isEditing = editing
                        print("isEditing\(isEditing)")
                    }
                )
                Text("\(lableText())")
                    .foregroundColor(isEditing ? .red : .blue)
            }.padding()
            
            Spacer()
        }.onReceive(self.data.$x, perform: { _ in
            self.updateBLE()
        })
        
    }
}

#Preview {
    ContentView()
}

